<?php
!defined('P_W') && exit('Forbidden');
$searchUrl = $basename.'q=friend&type=find&';
/**
 * 获取单个好友的信息
 *
 * @param int $friendid
 * @return array
 */
function getOneFriend($friendid){
	global $db,$winduid;
	!$winduid && Showmsg('not_login');
	$friendid = (int) $friendid;
	$friend = $db->get_one("SELECT m.uid,m.username,m.icon,m.honor,md.f_num FROM pw_friends f LEFT JOIN pw_members m ON f.friendid=m.uid LEFT JOIN pw_memberdata md ON f.friendid=md.uid WHERE f.uid=".pwEscape($winduid)." AND f.friendid=".pwEscape($friendid)." AND f.status=0");
	if ($friend) {
		require_once(R_P.'require/showimg.php');
		list($friend['face']) = showfacedesign($friend['icon'],1);
		$friend['honor'] = substrs($friend['honor'],90);
		return $friend;
	} else {
		return false;
	}
}

function isFriend($uid,$friend) {
	global $db;
	if ($db->get_value("SELECT uid FROM pw_friends WHERE uid=" . pwEscape($uid) . ' AND friendid=' . pwEscape($friend) . " AND status='0'")) {
		return true;
	}
	return false;
}

function getOneInfo($uid){
	global $db;
	$uid = (int) $uid;
	if (!$uid) Showmsg('undefined_action');
	$oneinfo = $db->get_one("SELECT m.uid,m.username,m.email,m.groupid,m.memberid,m.icon,m.gender,m.regdate,m.honor,m.bday,m.medals,m.userstatus,md.thisvisit,md.onlinetime,md.postnum,md.digests,md.rvrc,md.money,md.credit,md.lastvisit,md.lastpost,md.todaypost,md.f_num FROM pw_members m LEFT JOIN pw_memberdata md ON m.uid=md.uid WHERE m.uid=".pwEscape($uid));
	if ($oneinfo) {
		require_once(R_P.'require/showimg.php');
		list($oneinfo['face']) = showfacedesign($oneinfo['icon'],1);
		$oneinfo['honor'] = substrs($oneinfo['honor'],90);
		return $oneinfo;
	} else {
		return false;
	}
}
/**
 * 获取相册最后上传的图片
 *
 * @param int $aid 相册id
 * @param int $num 获取数量
 * @return array
 */
function getLastPid($aid, $num = 5) {
	global $db;
	$lastpid = array();
	$query = $db->query("SELECT pid FROM pw_cnphoto WHERE aid=" . pwEscape($aid) . " ORDER BY pid DESC LIMIT $num");
	while ($rt = $db->fetch_array($query)) {
		$lastpid[] = $rt['pid'];
	}
	return $lastpid;
}

/**
 * 获取相片的真实路径
 *
 * @param string $path
 * @param bool $thumb
 * @return string
 */
function getphotourl($path,$thumb = false) {
	global $pwModeImg;
	if (!$path) {
		return "$pwModeImg/nophoto.gif";
	}
	$lastpos = strrpos($path,'/') + 1;
	$thumb && $path = substr($path, 0, $lastpos) . 's_' . substr($path, $lastpos);
	list($path) = geturl($path, 'show');
	if ($path == 'imgurl' || $path == 'nopic') {
		return "$pwModeImg/nophoto.gif";
	}
	return $path;
}

/**
 * 获取用户的唯一字符串
 */
function appkey($uid,$app=false,$add=false){
	global $db_hash;
	return substr(md5($uid.$db_hash.($add ? $add : '').($app ? $app : '')),8,18);
}

/**
 * share应用动态解析
 */
function parseFeed($message){
	$message = descriplog($message);
	if (strpos($message,'[/SHARE]')!==false || strpos($message,'[/share]')!==false) {
		$message = preg_replace("/\[share\]([\w\.]+),([^\[,]+?),([\d]{1,10})\[\/share\]/eis","shareEvea('\\1','\\2','\\3')",$message);
	}
	if (strpos($message,'[/IMG]')!==false || strpos($message,'[/img]')!==false) {
		$message = preg_replace("/\[img\](https?)?([^\[]+?)\[\/img\]/is","<img src=\"\\1\\2\" />",$message);
	}
	return $message;
}
function shareEvea($type,$hash,$id){
	global $db_mode,$pwModeImg;
	static $share_js = 0;
	if (strpos($type,'.')) {
		$src = "$pwModeImg/share_v.png";
	} elseif ($type == 'music') {
		$src = "$pwModeImg/musicplay.gif";
	} elseif ($type == 'flash') {
		$src = "$pwModeImg/musicplay.gif";
	} else {
		$src = '';
	}
	if ($src) {
		return (++$share_js ? '<script id="share_js" src="mode/'.$db_mode.'/js/share.js"></script>' : '').'<img style="cursor: pointer;" src="'.$src.'" onclick="javascript:showShare(\''.$type.'\',\''.$hash.'\',this,\''.$id.'\')"/>';
	} else {
		return '';
	}
}

/**
 * 获取各个评论类型的数据库表名称
 *
 * @param string $type
 * @return string
 */
function getCommTypeTable($type){
	switch ($type) {
		case 'share':
			$app_table = 'pw_share';
			$id_filed = 'id';
			break;
		case 'write':
			$app_table = 'pw_owritedata';
			$id_filed = 'id';
			break;
		case 'photo':
			$app_table = 'pw_cnphoto';
			$id_filed = 'pid';
			break;
		case 'board':
			$app_table = 'pw_oboard';
			$id_filed = 'id';
			break;
		case 'diary':
			$app_table = 'pw_diary';
			$id_filed = 'did';
			break;
		default:
			return false;
	}
	return array($app_table,$id_filed);
}

/**
 * 确认是否开启该评论
 *
 * @param string $type
 * @return bool
 */
function checkCommType($type){
	return in_array($type,array('share','write','photo','board','diary'));
}

function delAppAction($type,$typeid){
	delFeed($type,$typeid);
	return delComment($type,$typeid);
}

function delComment($type,$typeid){
	global $db;
	$affected_rows = 0;
	if (checkCommType($type)){
		$db->update("DELETE FROM pw_comment WHERE type=".pwEscape($type)." AND typeid=".pwEscape($typeid));
		$affected_rows = $db->affected_rows();
	}
	return $affected_rows;
}
function delFeed($type,$typeid){
	global $db;
	$db->update("DELETE FROM pw_feed WHERE type=".pwEscape($type)." AND typeid=".pwEscape($typeid));
	$affected_rows = $db->affected_rows();
	return $affected_rows;
}
/**
 * 取得某UID用户对当前登录用户的隐私处理
 *
 *	$isU:	2 自己	1 朋友	0 非已非友
 *	$privacy:	各各隐私设置针对当前UID是否生效
 * @param  int $uid
 */
function pwUserPrivacy($uid,$userdb) {
	global $winduid;
	$isU = $uid == $winduid ? 2 : 0;
	if (!$isU) {
		$isU = isFriend($uid,$winduid) ? 1 : 0;
	}

	$privacy['index'] = ((int)$userdb['index_privacy'] <= $isU) ? true : false;
	if ($privacy['index']) {
		$privacy['profile']	= ((int)$userdb['profile_privacy'] <= $isU) ? true : false;
		$privacy['info']	= ((int)$userdb['info_privacy'] <= $isU) ? true : false;
		$privacy['credit']	= ((int)$userdb['credit_privacy'] <= $isU) ? true : false;
		$privacy['owrite']	= ((int)$userdb['owrite_privacy'] <= $isU) ? true : false;
		$privacy['msgboard']= ((int)$userdb['msgboard_privacy'] <= $isU) ? true : false;
	} else {
		$privacy['profile']	= false;
		$privacy['info']	= false;
		$privacy['credit']	= false;
		$privacy['owrite']	= false;
		$privacy['msgboard']= false;
	}
	return array($isU,$privacy);
}

/**
 * 圈子中各种用户发表信息统计，记录、分享、群组、相册、回复等
 *
 * @param string $exp 表达式，包含+或-符号
 */
function countPosts($exp='+1') {
	global $db;
	$num = intval(trim($exp,'+-'));
	if (strpos($exp,'+') !== false) {
		$db->update("UPDATE pw_bbsinfo SET o_post=o_post+".pwEscape($num,false).",o_tpost=o_tpost+".pwEscape($num,false));
	} else {
		$db->update("UPDATE pw_bbsinfo SET o_post=o_post-".pwEscape($num,false).",o_tpost=o_tpost-".pwEscape($num,false));
	}
}

function creatFriendsHeader($u){
	global $db_dopen,$db_phopen,$o_share_open,$o_groups_open,$basename,$q,$see;
	$ckq = $q;
	$see == 'post' && $q == 'article' && $ckq = 'articlepost';
	if($see == 'post' && $q == 'article'){
		$ckq = 'articlepost';
	} elseif ($see == 'favor' && $q == 'article') {
		$ckq = 'articlefavor';
	} elseif ($see == 'trade' && $q == 'article') {
		$ckq = 'articletrade';
	}
	$html = "<li id=\"friendheader_write\"><a href=\"{$basename}q=write&u=$u\">".getLangInfo('other','write')."</a></li>";
	if ($db_dopen) {
		$html .= "<li id=\"friendheader_diary\"><a href=\"{$basename}q=diary&u=$u\">".getLangInfo('other','diary')."</a></li>";
	}
	if ($db_phopen) {
		$html .= "<li id=\"friendheader_photos\"><a href=\"{$basename}q=photos&u=$u\">".getLangInfo('other','album')."</a></li>";
	}
	if ($o_share_open) {
		$html .= "<li id=\"friendheader_share\"><a href=\"{$basename}q=share&u=$u\">".getLangInfo('other','share')."</a></li>";
	}
	
	$html .= "<li id=\"friendheader_article\"><a href=\"{$basename}q=article&u=$u\">".getLangInfo('other','article')."</a></li>";
	$html .= "<li id=\"friendheader_articlepost\"><a href=\"{$basename}q=article&see=post&u=$u\">".getLangInfo('other','articlepost')."</a></li>";
	$html .= "<li id=\"friendheader_articlefavor\"><a href=\"{$basename}q=article&see=favor&u=$u\">".getLangInfo('other','articlefavor')."</a></li>";
	$html .= "<li id=\"friendheader_articletrade\"><a href=\"{$basename}q=article&see=trade&u=$u\">".getLangInfo('other','articletrade')."</a></li>";
	$html .= "<li id=\"friendheader_friend\"><a href=\"{$basename}q=friend&u=$u\">".getLangInfo('other','friend')."</a></li>";
	if($o_groups_open){
		$html .= "<li id=\"friendheader_groups\"><a href=\"{$basename}q=groups&u=$u\">".getLangInfo('other','groups')."</a></li>";
	}
	$html .= "<script language=\"JavaScript\">setCurrent('friendheader_$ckq','','current');</script>";
	return $html;
}

function getAppClassName($type){
	if ($type=='share') {
		return 'i-o';
	} elseif ($type=='board') {
		return 'i-b';
	} elseif ($type=='friend') {
		return 'i-d';
	} elseif ($type=='photo') {
		return 'i-j';
	} elseif ($type=='post') {
		return 'i-a';
	} elseif ($type=='write') {
		return 'i-k';
	} elseif ($type=='diary') {
		return 'i-t';
	} elseif ($type=='colony'){
		return 'i-e';
	} else {
		return 'i-a';
	}
}

function banUser(){
	global $db,$groupid,$winduid,$timestamp;
	if ($groupid == 6) {
		$pwSQL = '';
		$flag  = 0;
		$bandb = $delban = array();
		$query = $db->query("SELECT * FROM pw_banuser WHERE uid=".pwEscape($winduid)." AND fid='0'");
		while ($rt = $db->fetch_array($query)) {
			if ($rt['type'] == 1 && $timestamp - $rt['startdate'] > $rt['days']*86400) {
				$delban[] = $rt['id'];
			} else {
				$bandb = $rt;
			}
		}
		$delban && $db->update('DELETE FROM pw_banuser WHERE id IN('.pwImplode($delban).')');
		($groupid == 6 && !isset($bandb)) && $pwSQL .= "groupid='-1',";
		if ($pwSQL = rtrim($pwSQL,',')) {
			$db->update("UPDATE pw_members SET $pwSQL WHERE uid=".pwEscape($winduid));
		}
		if ($bandb) {
			if ($bandb['type'] == 1) {
				$s_date = get_date($bandb['startdate']);
				$e_date = $bandb['startdate'] + $bandb['days']*86400;
				$e_date = get_date($e_date);
				Showmsg('ban_info1');
			} else {
				if ($bandb['type'] == 3) {
					Cookie('force',$winduid);
					Showmsg('ban_info3');
				} else {
					Showmsg('ban_info2');
				}
			}
		}
	}
	if (GetCookie('force') && $winduid != GetCookie('force')) {
		$force = GetCookie('force');
		$bandb = $db->get_one("SELECT type FROM pw_banuser WHERE uid=".pwEscape($force)." AND fid='0'");
		if ($bandb['type'] == 3) {
			Showmsg('ban_info3');
		} else {
			Cookie('force','',0);
		}
	}
	/**
	* 需要验证用户只有通过管理员验证后才能发帖
	*/
	if ($groupid == '7') {
		Showmsg('post_check');
	}
}

function appShield($code) {
	$code = getLangInfo('other',$code);
	return "<span style=\"color:black;background-color:#ffff66\">$code</span>";
}

function banFriend() {
	
}
?>